package com.zdsoft.blp.zongbuju.viewpage.fragMent;


import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.view.MotionEvent;
import android.view.View;

import android.view.animation.Animation;
import android.view.animation.ScaleAnimation;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.youth.banner.Banner;
import com.youth.banner.BannerConfig;
import com.zdsoft.blp.R;
import com.zdsoft.blp.gongyong.API.API;
import com.zdsoft.blp.gongyong.dialog.ShowOrDismissDialog;
import com.zdsoft.blp.gongyong.isLoginSuccess.IsLoginSuccess;
import com.zdsoft.blp.gongyong.myFragment.CustomFragment;
import com.zdsoft.blp.gongyong.net.Net;
import com.zdsoft.blp.gongyong.net.NetCallback;
import com.zdsoft.blp.zongbuju.MainActivity;
import com.zdsoft.blp.zongbuju.shouye.bianmin.activity.BMxqActivity;
import com.zdsoft.blp.zongbuju.shouye.bianmin.activity.BianminActivity;

import com.zdsoft.blp.zongbuju.shouye.entity.BannerInfo;
import com.zdsoft.blp.zongbuju.shouye.fankui.FanKuiActivity;
import com.zdsoft.blp.zongbuju.shouye.linliquan.activity.LinLiQuanActivity;
import com.zdsoft.blp.zongbuju.shouye.taoshihui.activity.ShangpxqActivity;
import com.zdsoft.blp.zongbuju.shouye.taoshihui.activity.TaoshActivity;
import com.zdsoft.blp.zongbuju.shouye.wuye.activity.WuYeActivity;
import com.zdsoft.blp.zongbuju.wode.activity.WDXuanZeSheQu;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * Created by pc on 2016/8/30.
 */
public class ShouYeFragment extends CustomFragment implements View.OnClickListener, View.OnTouchListener, NetCallback {
    private LinearLayout ll_bianmin, ll_wuye, ll_taoshihui, ll_linliquan, ll_fankui, ll_xzshequ;
    private Banner banner;
    private TextView tv_xiaoqu;
    private SharedPreferences sp;
    private static final Integer HUOQULUBBO = 1;
    private List<BannerInfo> lists = null;
    @Override
    protected void init() {
        initLayout(R.layout.shouye);
        ll_bianmin = find(R.id.ll_bianmin);
        ll_wuye = find(R.id.ll_wuye);
        ll_taoshihui = find(R.id.ll_taoshihui);
        ll_linliquan = find(R.id.ll_linliquan);
        ll_fankui = find(R.id.ll_fankui);
        ll_xzshequ = find(R.id.ll_xzshequ);
        tv_xiaoqu = find(R.id.tv_xiaoqu);

        banner = find(R.id.banner);
        banner.setDelayTime(2000);
        banner.setBannerStyle(BannerConfig.CIRCLE_INDICATOR);


        banner.setOnBannerClickListener(new Banner.OnBannerClickListener() {
            @Override
            public void OnBannerClick(View view, int position) {
                Toast.makeText(getActivity(), position + "", Toast.LENGTH_SHORT).show();
                if (lists.size() > 0) {
                    if("goods".equals(lists.get(position-1).getType())){
                        Intent intent = new Intent(getActivity(),ShangpxqActivity.class);
                        intent.putExtra("goodsId",lists.get(position-1).getObjId().toString());
                        startActivity(intent);
                    }else if("info".equals(lists.get(position-1).getType())){
                        Intent intent = new Intent(getActivity(), BMxqActivity.class);
                        intent.putExtra("id", lists.get(position-1).getObjId().toString());
                        startActivity(intent);
                    }
                }
            }
        });
        //请求轮播广告信息

        ll_bianmin.setOnClickListener(this);
        ll_wuye.setOnClickListener(this);
        ll_taoshihui.setOnClickListener(this);
        ll_linliquan.setOnClickListener(this);
        ll_fankui.setOnClickListener(this);
        ll_xzshequ.setOnClickListener(this);

        ll_bianmin.setOnTouchListener(this);
        ll_wuye.setOnTouchListener(this);
        ll_taoshihui.setOnTouchListener(this);
        ll_linliquan.setOnTouchListener(this);

    }


    @Override
    public void onClick(View v) {
        Intent intent = null;
        switch (v.getId()) {
            case R.id.ll_fankui:
                if (IsLoginSuccess.isloginSuccess(getActivity(), false)) {
                    intent = new Intent(getActivity(), FanKuiActivity.class);
                }
                break;
            case R.id.ll_xzshequ:
                intent = new Intent(getActivity(), WDXuanZeSheQu.class);
                intent.putExtra("type", 2);
                break;
        }
        if (intent != null) {
            startActivity(intent);
        }
    }

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        Intent intent = null;
        ScaleAnimation scaleAnimation = null;
        if (event.getAction() == MotionEvent.ACTION_UP) {
            scaleAnimation = new ScaleAnimation(0.95f, 1, 0.95f, 1, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
            scaleAnimation.setDuration(200);
            scaleAnimation.setFillAfter(true);
            scaleAnimation.setInterpolator(getActivity(), android.R.interpolator.accelerate_quint);
        } else if (event.getAction() == MotionEvent.ACTION_DOWN) {
            scaleAnimation = new ScaleAnimation(1, 0.95f, 1, 0.95f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
            scaleAnimation.setDuration(200);
            scaleAnimation.setFillAfter(true);
            scaleAnimation.setInterpolator(getActivity(), android.R.interpolator.accelerate_quint);
        }
        if (scaleAnimation != null) {
            switch (v.getId()) {
                case R.id.ll_bianmin:
                    ll_bianmin.startAnimation(scaleAnimation);
                    intent = new Intent(getActivity(), BianminActivity.class);

                    break;
                case R.id.ll_wuye:
                    ll_wuye.startAnimation(scaleAnimation);
                    intent = new Intent(getActivity(), WuYeActivity.class);
                    break;
                case R.id.ll_linliquan:
                    ll_linliquan.startAnimation(scaleAnimation);
                    intent = new Intent(getActivity(), LinLiQuanActivity.class);
                    break;
                case R.id.ll_taoshihui:
                    ll_taoshihui.startAnimation(scaleAnimation);
                    intent = new Intent(getActivity(), TaoshActivity.class);
                    break;
            }
            if (event.getAction() == MotionEvent.ACTION_UP && intent != null) {
               /* if(event.getX()>ll_bianmin.getX()&&event.getY()>ll_bianmin.getY()){
                    return true;
                }
                if(event.getX()>ll_wuye.getX()&&event.getY()>ll_wuye.getY()){
                    return true;
                }*/
                startActivity(intent);
            }
        }
        return true;
    }

    public void getLunBoInfo() {
        sp = getActivity().getSharedPreferences("info", Context.MODE_PRIVATE);
        String code = sp.getString("communityCode", "");
        tv_xiaoqu.setText(sp.getString("communityName", ""));
        if (!"".equals(code)) {
            ShowOrDismissDialog.show(getActivity());
            Net net = new Net();
            net.url = API.TAKETURNS_URL;
            net.nameSpace = API.TAKETURNS_NAMESPACE;
            net.methodName = API.TAKETURNS_LIST;
            Map<String, Object> map = new HashMap<>();
            map.put("communityCode", code);
            net.parms = map;
            net.code = HUOQULUBBO;
            ((MainActivity) getActivity()).getServer(net, this);
        } else if(sp.getBoolean("isFirstChooseXx",true)){
            Intent intent = new Intent(getActivity(), WDXuanZeSheQu.class);
            intent.putExtra("type", 2);
            startActivity(intent);
            setBannerImage();
        }
    }

    @Override
    public void onResponse(boolean flag, int code, String res) {
        if (flag && code == HUOQULUBBO) {
            try {
                lists = new ArrayList<>();
                JSONArray array = new JSONArray(res);
                for (int i = 0; i < array.length(); i++) {
                    JSONObject object = array.getJSONObject(i);
                    lists.add(new BannerInfo(object.getString("title"), object.getString("filePath"), object.getString("objId"), object.getString("type")));
                }
                setBannerImage();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        ShowOrDismissDialog.dismiss();
    }

    private void setBannerImage() {
        List<String> list = new ArrayList<>();
        if (lists != null && lists.size() != 0) {
            for (int i = 0; i < lists.size(); i++) {
                list.add(lists.get(i).getImageUrl());
            }
        } else {
            list.add("http://114.215.193.46:8080/uploadFiles/sylh/2015/09/20150924103535403");
        }
        banner.setImages(list);
    }



    @Override
    public void onPause() {
        super.onPause();
        SharedPreferences.Editor editor = sp.edit();
        editor.putBoolean("isFirstChooseXx",false);
        editor.commit();
    }

    @Override
    public void onStart() {
        super.onStart();
        getLunBoInfo();
    }
}